package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionMapper;
import com.itheima.health.dao.RolePermissionMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
@Slf4j
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    /**
     * @Author: JASON
     * @Date: 19:35 2021/1/16
     * @Parms [queryPageBean]
     * @ReturnType: java.util.List<com.itheima.health.pojo.Permission>
     * @Description: 权限分页查询
    */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("[权限控制-分页查询service]data：{}",queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> permissions = permissionMapper.findByPage(queryPageBean.getQueryString());
        log.info("[权限查询结果]data：{}",permissions);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(permissions.getTotal());
        pageResult.setRows(permissions.getResult());
        return pageResult;
    }

    /**
     * @Author: JASON
     * @Date: 20:07 2021/1/16
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.Permission
     * @Description: 根据id查找权限
    */
    @Override
    public Permission findById(Integer id) {
        log.info("Service[权限]根据id查找数据"+id);
        return permissionMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 20:11 2021/1/16
     * @Parms [permission]
     * @ReturnType: void
     * @Description: 修改权限
    */
    @Override
    public void edit(Permission permission) {
        log.info("Service[权限项]编辑"+permission);
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    /**
     * @Author: JASON
     * @Date: 20:31 2021/1/16
     * @Parms []
     * @ReturnType: java.util.List<com.itheima.health.pojo.Permission>
     * @Description: 查询所有权限
    */
    @Override
    public List<Permission> findAll() {
        return permissionMapper.selectAll();
    }

    /**
     * @Author: JASON
     * @Date: 20:31 2021/1/16
     * @Parms [id]
     * @ReturnType: void
     * @Description: 删除权限
    */
    @Override
    public void delete(Integer id) {
        log.info("Service[权限项]根据id删除数据"+id);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(id);
        int count = rolePermissionMapper.selectCount(rolePermission);
        if(count>0){
            throw new BusinessException("该检查项有关联数据不可以删除");
        }
        permissionMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 20:31 2021/1/16
     * @Parms [permission]
     * @ReturnType: void
     * @Description: 添加权限
    */
    @Override
    public void add(Permission permission) {
        log.info("Service[权限]添加数据"+permission);
        permissionMapper.insertSelective(permission);
    }
}
